package HomeWork.hw1;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author Administrator
 * @Date 2020/5/19 13:41
 **/
@WebServlet("/storyServlet")
public class Story extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*获取请求头referer（告诉服务器从哪来）
        2.判断，若是从本项目地址过来的，
        则请求转发到storydetail.html
        否则转发到errorStory.html
         */
        String referer =req.getHeader("referer");
        //获取项目资源路径
        String contextPath=req.getContextPath();
        if (referer!=null){
            if (referer.contains(contextPath)){
                req.getRequestDispatcher("/HomeWork/storyTrue.html");
            }else {
                req.getRequestDispatcher("/HomeWork/errorStory.html");
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
